package com.zyh.um.controller;

import com.zyh.um.entity.User;
import com.zyh.um.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping(value = "/formUserManager")
public class FormUserController {
    @Resource
    private UserService service;

    @PostMapping(value = "/registerUser")
    public User registerUser(User user, MultipartFile multipartFile) throws IOException {
        String filename = multipartFile.getOriginalFilename();
        user.setPhoto(filename);
        service.saveUser(user);
        return user;
    }

    @PostMapping(value = "/userLogin")
    public User userLogin(@RequestBody User user){
        System.err.println("user = " + user);
        return service.queryUserByNameAndPassword(user);
    }
 
    @PutMapping(value = "/updateUser")
    public void updateUser(User user,MultipartFile multipartFile) throws IOException{
        if (multipartFile!=null && multipartFile.getOriginalFilename()!=null){
            user.setPhoto(multipartFile.getOriginalFilename());
        }
        service.updateUser(user);
    }

    @DeleteMapping(value = "/deleteUserByIds")
    public void deleteUserByIds(Integer[] ids){
        service.deleteByUserIds(ids);
    }

    @GetMapping(value = "/queryUserByPage")
    public List<User> queryUserByPage(@RequestParam(value = "pageIndex",defaultValue = "1") Integer pageIndex
            ,@RequestParam(value = "rows",defaultValue = "10") Integer pageSize
            ,@RequestParam(value = "column",required = false) String column
            ,@RequestParam(value = "value",required = false) String value){
     return service.queryUserByPage(pageIndex,pageSize,column,value);
    }

    @GetMapping(value = "/queryUserCount")
    public Integer queryUserCount(String column,String value){return service.queryUserCount(column,value);}

    @GetMapping(value = "/queryUserById")
    public User queryUserById(Integer id){
        return service.queryUserById(id);
   }
 
}